#ifndef _RGBLINK_PROTOCOL_H_
#define _RGBLINK_PROTOCOL_H_

#include "define.h"
#include "pipe.h"


typedef void (*RGBlinkProtocolCallback)(BYTE *receiveBuffer);
typedef void (*SendBufferFunc)(char * buffer, int size);

typedef struct
{
	BYTE address;
	BYTE sn;
	BYTE command;
	BYTE data_1;
	BYTE data_2;
	BYTE data_3;
	BYTE data_4;
	BYTE checksum;
} Command_TypeDef;


#define MAX_RGBLNK_DEVICE 5

enum {
	
	CMD_INDEX_ADDRESS,
	CMD_INDEX_SN,
	CMD_INDEX_CMD,
	CMD_INDEX_DAT1,
	CMD_INDEX_DAT2,
	CMD_INDEX_DAT3,
	CMD_INDEX_DAT4,
	CMD_INDEX_CHECKSUM,
	CMD_INDEX_SIZE,
	
};

typedef struct {
	BYTE isConnect[MAX_RGBLNK_DEVICE];
	BYTE index;
	BYTE state;
	BYTE sendSn[MAX_RGBLNK_DEVICE];
	BYTE receiveSn[MAX_RGBLNK_DEVICE];
	BYTE hexCharBuffer[2];
	BYTE receiveBuffer[CMD_INDEX_SIZE];	
	BYTE receiveCmd[MAX_RGBLNK_DEVICE][CMD_INDEX_SIZE];
	RGBlinkProtocolCallback callback;
	SendBufferFunc sendBuffer;
	Pipe_t *pipe;
}RGBlinkProtocol;

typedef struct{
	
	BYTE state;
	BYTE index;
	BYTE hexChar[2];
	BYTE cmd[CMD_INDEX_SIZE];
} InterpretStateType;

bool interpret_receive_char(InterpretStateType * protocol, BYTE value);



void Init_RGBlinkProtocol(RGBlinkProtocol * protocol, Pipe_t *pipe, RGBlinkProtocolCallback callback, SendBufferFunc sendBuffer );
void Task_RGBlinkProtocol(RGBlinkProtocol * protocol);
int  SendCmd_RGBlinkProtocol(RGBlinkProtocol * protocol, BYTE add, BYTE cmd, BYTE dat1, BYTE dat2, BYTE dat3, BYTE dat4);
int  SendCmdNoWait_RGBlinkProtocol(RGBlinkProtocol * protocol, BYTE add, BYTE cmd, BYTE dat1, BYTE dat2, BYTE dat3, BYTE dat4);
int  SendCmdAndWait_RGBlinkProtocol(RGBlinkProtocol * protocol, BYTE add, BYTE cmd, BYTE dat1, BYTE dat2, BYTE dat3, BYTE dat4);
void SendBuffer_RGBlinkProtocol(RGBlinkProtocol * protocol, char *buffer, int size);
int ReceiveBuffer_RGBlinkProtocol(RGBlinkProtocol * protocol, char *buffer, int size, int timeout);
int WaitAck_RgblinkProtocol(RGBlinkProtocol * protocol, BYTE add, int timeout);
int  SendCmdAndDataBlock_RGBlinkProtocol(RGBlinkProtocol * protocol, BYTE add, BYTE cmd, BYTE dat1, BYTE dat2, BYTE dat3, BYTE dat4, char *buffer,int size);
int SendCmdIsAck_RGBlinkProtocol(RGBlinkProtocol * protocol, BYTE add);








#endif

